package traveldream.ejb.entities;

import java.io.Serializable;

import javax.persistence.*;

import traveldream.ejb.DTO.BookingDTO;

import java.util.Date;


/**
 * The persistent class for the booking database table.
 * 
 */
@Entity
@NamedQueries({
@NamedQuery(name="Booking.findAll", query="SELECT b FROM Booking b"),
@NamedQuery(name="Booking.checkAvl", query="SELECT DISTINCT h FROM Booking b JOIN Hotel h WHERE h.Invalid=false AND h.city = :arr_place AND h.available_Places >= (SELECT sum(b.number_of_People) FROM Booking b WHERE b.departure_Date <= :arr_date AND b.arrival_Date >= :dep_date AND b.hotel=h)+:n"),
@NamedQuery(name="Booking.findAvl", query="SELECT SUM(b.number_of_People) FROM Booking b WHERE b.departure_Date <= :arr_date AND b.arrival_Date >= :dep_date AND b.hotel= :h")
})
public class Booking implements Serializable {
	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	@Column(name="`Booking Number`")
	private int booking_Number;

	@Temporal(TemporalType.DATE)
	@Column(name="`Arrival Date`")
	private Date arrival_Date;

	@Temporal(TemporalType.DATE)
	@Column(name="`Departure Date`")
	private Date departure_Date;

	@Column(name="`Number of People`")
	private int number_of_People;

	//bi-directional many-to-one association to Hotel
	@ManyToOne
	@JoinColumn(name="Id")
	private Hotel hotel;

	public Booking() {
	}
	
	public void convertFromDTO(BookingDTO bDTO)
	{
		Hotel h=new Hotel(bDTO.gethDTO());
		h.setId(bDTO.gethDTO().getId());
		this.setHotel(h);
		this.setDeparture_Date(bDTO.getgDTO().getArrivalDate()); //the booking at the hotel start when i arrive at the destination
		this.setArrival_Date(bDTO.getrDTO().getDepartureDate()); //i leave the hotel the same day i leave from the city
		this.setNumber_of_People(bDTO.getnOfPeople());
	}
	
	public int getBooking_Number() {
		return this.booking_Number;
	}

	public void setBooking_Number(int booking_Number) {
		this.booking_Number = booking_Number;
	}

	public Date getArrival_Date() {
		return this.arrival_Date;
	}

	public void setArrival_Date(Date arrival_Date) {
		this.arrival_Date = arrival_Date;
	}

	public Date getDeparture_Date() {
		return this.departure_Date;
	}

	public void setDeparture_Date(Date departure_Date) {
		this.departure_Date = departure_Date;
	}

	public int getNumber_of_People() {
		return this.number_of_People;
	}

	public void setNumber_of_People(int number_of_People) {
		this.number_of_People = number_of_People;
	}

	public Hotel getHotel() {
		return this.hotel;
	}

	public void setHotel(Hotel hotel) {
		this.hotel = hotel;
	}

}